package com.ybt.communicate.tcp.accessor.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.communicate.tcp.accessor.IDataAccessStrategy;

public class LengthUnknownAccessor implements IDataAccessStrategy {
    private String strategy;
    private LengthUnknownAccessor(String strategy){
        this.strategy=strategy;
    }
    /* (non-Javadoc)
     * @see com.ybt.communicate.tcp.accessor.IDataAccessStrategy#send(java.nio.channels.WritableByteChannel, java.lang.String)
     */
    public void send(WritableByteChannel channel, String data)
            throws ProtocolException {
        // TODO Auto-generated method stub
        /*try {
            //send(channel, data.getBytes(charset));
        } catch (UnsupportedEncodingException e) {
            throw new ProtocolException(e);
        }*/
    }
    
    /* (non-Javadoc)
     * @see com.wx.workflow.communicate.tcp.data.DataAccessStrategy#receive(java.nio.channels.ReadableByteChannel)
     */
    public byte[] receive(ReadableByteChannel channel) throws ProtocolException {
        // TODO Auto-generated method stub
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        WritableByteChannel out = Channels.newChannel(result);
        try {
            while (channel.read(buffer) != -1) {
                //数据还没有读完
                if (!buffer.hasRemaining()) {
                    //buffer已填充满
                    //准备读取缓冲区
                    buffer.flip();
                    //读取缓冲区
                    out.write(buffer);
                    //清空缓冲区
                    buffer.clear();
                }
            }

            //读取最后的缓冲部分数据
            buffer.flip();
            //读取缓冲区
            out.write(buffer);
            //清空缓冲区
            buffer.clear();
            return result.toByteArray();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new ProtocolException(e);
        }
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.communicate.tcp.data.DataAccessStrategy#send(java.nio.channels.WritableByteChannel, byte[])
     */
    public void send(WritableByteChannel client, byte[] data)
            throws ProtocolException {
        // TODO Auto-generated method stub
        ByteBuffer buffer = ByteBuffer.wrap(data);
        try {
            while (buffer.hasRemaining()){
                //buffer还没有写完(非阻塞io不一定一次写完)
                client.write(buffer);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new ProtocolException(e);
        }
    }

    public static LengthUnknownAccessor newInstance(String strategy) {
        return new LengthUnknownAccessor(strategy);
    }
    
    public static void main(String[] args){
        ByteArrayOutputStream in = new ByteArrayOutputStream();
        WritableByteChannel out = Channels.newChannel(in);
        LengthUnknownAccessor acc = LengthUnknownAccessor.newInstance(null);
        try {
            acc.send(out,"123456789".getBytes());
            System.out.println(new String(in.toByteArray()));
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
